package com.primaryedu.ui.center;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.primaryedu.R;
import com.primaryedu.data.Contants;
import com.primaryedu.data.GlobalData;
import com.primaryedu.ui.BaseAppCompatActivity;
import com.primaryedu.ui.app.nicedialog.BaseNiceDialog;
import com.primaryedu.ui.app.nicedialog.DialogCallBackUtils;
import com.primaryedu.ui.app.nicedialog.DialogSureCallBack;
import com.primaryedu.ui.app.nicedialog.MyCustomDialog;

/**
 * 意见反馈
 *
 * @author jun.wang
 * @description
 * @data 2017年1月6日
 */
public class OpinionActivity extends BaseAppCompatActivity implements OnClickListener, DialogSureCallBack {
    private EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_opinion);

    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_opinion;
    }

    @Override
    protected void initView() {
        DialogCallBackUtils.setCallBack(this);
        ((TextView) findViewById(R.id.title)).setText("意见反馈");
        findViewById(R.id.back).setOnClickListener(this);
//        findViewById(R.id.next).setOnClickListener(this);
        edit = (EditText) this.findViewById(R.id.edit);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;

//            case R.id.next:
//                next();
//                break;

            default:
                break;
        }
    }

    public void showdialogsaveAdvice(View view) {
        if (TextUtils.isEmpty(edit.getText().toString())) {
            showToast("请填写您的意见！");
            return;
        }

        MyCustomDialog.newInstance("saveAdvice")
                .setMargin(60)
                .setOutCancel(false)
                .show(getSupportFragmentManager());
    }

    @Override
    public void SureClick(String type, BaseNiceDialog dialog) {
        if (type.equals("saveAdvice")){
            next();
        }
        dialog.dismiss();
    }
    private void next() {
        showDialog();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Contants.saveAdvice,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        closeDialog();

                        try {
                            JSONObject json = new JSONObject(response);
                            String reason = json.getString("reason");
                            int result = json.getInt("result");

                            if (result == 1) {
                                showToast("提交成功！");
                                finish();
                            } else {
                                showToast(reason);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                closeDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //在这里设置需要post的参数
                Map<String, String> map = new HashMap<String, String>();
                map.put("userid", GlobalData.getUserId(OpinionActivity.this));
                map.put("content", edit.getText().toString());
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }



}
